/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * The Class MonthSchedule.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class MonthSchedule extends TimeSchedule {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 40L;

	/** The Constant JANUARY. */
	public static final int JANUARY = Calendar.JANUARY;

	/** The Constant FEBRUARY. */
	public static final int FEBRUARY = Calendar.FEBRUARY;

	/** The Constant MARCH. */
	public static final int MARCH = Calendar.MARCH;

	/** The Constant APRIL. */
	public static final int APRIL = Calendar.APRIL;

	/** The Constant MAY. */
	public static final int MAY = Calendar.MAY;

	/** The Constant JUNE. */
	public static final int JUNE = Calendar.JUNE;

	/** The Constant JULY. */
	public static final int JULY = Calendar.JULY;

	/** The Constant AUGUST. */
	public static final int AUGUST = Calendar.AUGUST;

	/** The Constant SEPTEMBER. */
	public static final int SEPTEMBER = Calendar.SEPTEMBER;

	/** The Constant OCTOBER. */
	public static final int OCTOBER = Calendar.OCTOBER;

	/** The Constant NOVEMBER. */
	public static final int NOVEMBER = Calendar.NOVEMBER;

	/** The Constant DECEMBER. */
	public static final int DECEMBER = Calendar.DECEMBER;

	/** The month selected. */
	private final boolean monthSelected[] = new boolean[12];

	/** The start day. */
	private int startDay;

	/** The start hour. */
	private int startHour;

	/** The start minute. */
	private int startMinute;

	/**
	 * Instantiates a new month schedule.
	 *
	 * @param month the month
	 * @param startDay the start day
	 * @param startHour the start hour
	 * @param startMinute the start minute
	 */
	public MonthSchedule(int month, int startDay, int startHour, int startMinute) {
		this.monthSelected[month] = true;
	}

	/**
	 * Instantiates a new month schedule.
	 *
	 * @param months the months
	 * @param startDay the start day
	 * @param startHour the start hour
	 * @param startMinute the start minute
	 */
	public MonthSchedule(int months[], int startDay, int startHour, int startMinute) {
		for (final int month : months) {
			this.monthSelected[month] = true;
		}
	}

	/**
	 * Sets the month value.
	 *
	 * @param month the month
	 * @param value the value
	 */
	public void setMonthValue(int month, boolean value) {
		this.monthSelected[month] = value;
	}

	/**
	 * Sets the all months value.
	 *
	 * @param value the new all months value
	 */
	public void setAllMonthsValue(boolean value) {
		for (int i = 0; i < this.monthSelected.length; i++) {
			this.monthSelected[i] = value;
		}
	}

	@Override
	public boolean isDateIntoSchedule(Date date) {

		if (date == null) {
			return false;
		}

		final Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		if (this.monthSelected[calendar.get(Calendar.MONTH) - 1] == true) {
			if (this.getStartDay() == calendar.get(Calendar.DAY_OF_MONTH)) {
				if (this.getStartHour() == calendar.get(Calendar.HOUR_OF_DAY)) {
					if (this.getStartMinute() == calendar.get(Calendar.MINUTE)) {
						return true;
					}
				}
			}
		}

		return false;
	}

	@Override
	public boolean isDateIntoSchedule(Date date, Date lastDate) {

		final boolean result = this.isDateIntoSchedule(date);

		if (lastDate == null) {
			return result;
		}

		if (result == false) {
			return false;
		}

		final Calendar calendar = new GregorianCalendar();
		final Calendar lastCalendar = new GregorianCalendar();
		calendar.setTime(date);
		lastCalendar.setTime(lastDate);

		if ((lastCalendar.get(Calendar.YEAR) != calendar.get(Calendar.YEAR)) || (lastCalendar.get(Calendar.MONTH) != calendar.get(Calendar.MONTH))) {
			return true;
		}

		return false;
	}

	/**
	 * Gets the month selected.
	 *
	 * @return the month selected
	 */
	public boolean[] getMonthSelected() {
		return this.monthSelected;
	}

	/**
	 * Gets the start day.
	 *
	 * @return the start day
	 */
	public int getStartDay() {
		return this.startDay;
	}

	/**
	 * Gets the start hour.
	 *
	 * @return the start hour
	 */
	public int getStartHour() {
		return this.startHour;
	}

	/**
	 * Gets the start minute.
	 *
	 * @return the start minute
	 */
	public int getStartMinute() {
		return this.startMinute;
	}

	/**
	 * Sets the start day.
	 *
	 * @param startDay the new start day
	 */
	public void setStartDay(int startDay) {
		this.startDay = startDay;
	}

	/**
	 * Sets the start hour.
	 *
	 * @param startHour the new start hour
	 */
	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	/**
	 * Sets the start minute.
	 *
	 * @param startMinute the new start minute
	 */
	public void setStartMinute(int startMinute) {
		this.startMinute = startMinute;
	}
}
